<#assign title='个人业绩记录'>
<#include 'header.ftl'>
<div class="record">
  <div class="layui-card">
    <div class="layui-card-header">
      <div>
        <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="methods.add();">添加</button>
        <div class="pull-right">
          <select id="incident-select" class="form-select"></select>
        </div>
      </div>
    </div>
    <div class="layui-card-body">
      <table id="self-record"></table>

      <script type="text/html" id="record-tool">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
      </script>
    </div>
  </div>

  <script>
    var methods;
    layui.use(['layer', 'jquery', 'laytpl', 'form', 'table'], function () {
      var $ = layui.$;
      var form = layui.form;
      var layer = layui.layer;
      var table = layui.table;
      var laytpl = layui.laytpl;

      var items = [];

      table.render({
        elem: '#self-record',
        url: '${root}/item/record/self',
        method: 'post',
        page: true,
        cols: [[
          {title: '序号', type: 'numbers'},
          {field: 'name', title: '项目'},
          {title: '所属事件', templet: '<b><b class="layui-badge layui-bg-green">{{d.incident.name}}</b></b>'},
          {title: '级别', templet: '<b><b class="layui-badge layui-bg-cyan">{{d.rank.name}}</b></b>'},
          {title: '身份', width: 90, templet: '<b><b class="layui-badge layui-bg-green">{{getPos(d.place.place)}}</b></b>'},
          {field: 'score', title: '分值', width: 60},
          {title: '审核', width: 80, templet: "<i>{{d.audit?'<b class=\"layui-badge layui-bg-blue\">已审核</b>':'<b class=\"layui-badge\">未审核</b>'}}</i>"},
          {title: '立项时间', width: 105, templet: "<b>{{d.beginTime?d.beginTime:'<b class=\"layui-badge layui-bg-gray\">请忽略</b>'}}</b>"},
          {field: 'endTime', title: '结项时间', width: 105},
          {title: '操作', toolbar: '#record-tool'}
        ]],
        parseData: function (res) {
          return {
            code: 0,
            msg: res.message,
            count: (res.payload ? res.payload.totalElements : 0),
            data: (res.payload ? res.payload.content : 0)
          }
        }
      });

      methods = {
        add: function () {
          saveAndUpdateRecord(items, $, form, layer, laytpl, null, function () {
            table.reload('self-record');
          });
        },
        del: function (record) {
          layer.confirm('确定删除<b class="layui-badge">'+record.name+'</b>记录吗？', {title: '提示'}, function (index) {
            $.post("${root}/item/record/delete/" + record.id, function (data) {
              if (data.status === 200) {
                layer.msg('删除成功！', {time: 1200, icon: 6});
                table.reload('self-record');
              } else {
                layer.alert('删除失败: ' + data.message, {title: '错误信息', icon: 2});
              }
            })
          });
        },
        edit: function (record) {
          if (record.status >= 1) {
            layer.alert('以被统计过的记录无法编辑！', {icon: 4, title: '禁止提示'});
          } else {
            saveAndUpdateRecord(items, $, form, layer, laytpl, record, function () {
              table.reload('self-record');
            });
          }
        }
      };
      table.on('tool', function (o) {
        var mtd = methods[o.event];
        if (mtd != null) {
          mtd(o.data);
        }
      });

      var $incidentSelect = $('#incident-select'); // 加载评定事件
      $.post('${root}/incident/page', {page: 1, limit: 99999, status: 3}, function (data) {
        if (data.status === 200) {
          var incs = data.payload.content;
          var inc = '{{# layui.each(d.incs, function(index, val) { }}' +
                    ' <option value="{{val.id}}">{{val.name}}</option>' +
                    '{{# }); }}';
          laytpl(inc).render({incs: incs}, function (html) {
            $incidentSelect.html(html);
          });
        } else {
          $incidentSelect.html('<option>暂无事件</option>');
          layer.msg('暂无事件！');
        }
      });

      $incidentSelect.change(function (e) {
        var iid = e.target.value;
        table.reload('self-record', {where: {incidentId: iid}});
      });
    });
  </script>
</div>

<#include 'footer.ftl'>
